package cn.bugstack.dev.tech.test;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.transport.ChainingCredentialsProvider;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.ai.document.Document;
import org.springframework.ai.reader.tika.TikaDocumentReader;
import org.springframework.ai.transformer.splitter.TokenTextSplitter;
import org.springframework.ai.vectorstore.PgVectorStore;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.PathResource;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;

/**
 * @ClassName：JGitTest
 * @Author: zhaiy
 * @Date: 2025/7/14 21:55
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
@SpringBootTest
@RunWith(SpringRunner.class)
@Slf4j
public class JGitTest {

    @Resource
    private TokenTextSplitter tokenTextSplitter;
    @Resource
    private PgVectorStore pgVectorStore;

    @Test
    public void test_getGitRepository() throws GitAPIException {
        String repoURL = "https://gitcode.com/zhai997397944/rag-for-role.git";
        String localPath = "./cloned-repo";
        String username = "zhai997397944";
        String password = "Zzy5Ece1AGaKBzB8mHX1wbSt";
        log.info("克隆路径：{}", new File(localPath).getAbsolutePath());
        CloneCommand cloneCommand = Git.cloneRepository();
        cloneCommand.setURI(repoURL);
        cloneCommand.setDirectory(new File(localPath));
        cloneCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password));
        Git git = cloneCommand.call();
        git.close();

    }

    @Test
    public void test_storageFromGit() throws GitAPIException, IOException {
        Files.walkFileTree(Path.of("./cloned-repo"), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                log.info("文件路径:{}", file.toString());
                if (file.toString().contains(".git")){
                    return FileVisitResult.CONTINUE;
                }
                PathResource resource = new PathResource(file);
                TikaDocumentReader reader = new TikaDocumentReader(resource);
                List<Document> documents = reader.get();
                List<Document> splitDocuments = tokenTextSplitter.apply(documents);
                splitDocuments.forEach(document -> {
                    document.getMetadata().put("knowledge", "rag-for-role");
                });
                pgVectorStore.accept(splitDocuments);
                return FileVisitResult.CONTINUE;
            }
        });
    }
}
